package com.yuan.model_convenient.rest;


import com.yuan.model_common.dto.CommonQueryCriteria;
import com.yuan.model_convenient.domain.ConvenientActivity;
import com.yuan.model_convenient.domain.ConvenientActivityEnroll;
import com.yuan.model_convenient.service.ConvenientActivityEnrollService;
import com.yuan.model_convenient.service.ConvenientActivityService;
import com.yuan.model_convenient.service.dto.ConvenientActivityDto;
import com.yuan.model_convenient.service.dto.ConvenientActivityEnrollDto;
import com.yuan.model_convenient.service.dto.ConvenientActivityEnrollQueryCriteria;
import com.yuan.model_convenient.service.dto.ConvenientActivityQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.utils.PageResult;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Api(tags = "物业便民: 小区活动管理")
@RestController
@RequestMapping("/api/convenientActivity")
@RequiredArgsConstructor
public class ConvenientActivityController {

    private final ConvenientActivityService convenientActivityService;
    private final ConvenientActivityEnrollService convenientActivityEnrollService;

    @ApiOperation("导出/convenient/activity")
    @GetMapping(value = "/download")
    public void exportActivity(HttpServletResponse response, CommonQueryCriteria criteria) throws IOException {
//        convenientActivityService.download(convenientActivityService.queryAll(criteria), response);
    }


    @GetMapping("/findList")
    @Log("查询/api/convenient/activity")
    @ApiOperation("查询/api/convenient/activity")
    public ResponseEntity<PageResult<ConvenientActivityDto>> queryActivityList(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        PageResult<ConvenientActivityDto> ConvenientActivityDtoPageResult = convenientActivityService.queryAll(pageable);
        return new ResponseEntity<>(ConvenientActivityDtoPageResult, HttpStatus.OK);
    }

    @GetMapping("/findListByType")
    @Log("查询/api/convenient/activity/type")
    @ApiOperation("查询/api/convenient/activity/type")
    public ResponseEntity<PageResult<ConvenientActivityDto>> findListByType(ConvenientActivityQueryCriteria criteria, Pageable pageable) {
        PageResult<ConvenientActivityDto> convenientActivityDtoPageResult = convenientActivityService.queryAllByType(criteria, pageable);
        return new ResponseEntity<>(convenientActivityDtoPageResult, HttpStatus.OK);
    }

    @Log("新增/api/convenient/activity/enroll")
    @ApiOperation("新增/api/convenient/activity/enroll")
    @PostMapping("/createEnroll")
    public ResponseEntity<Object> createActivityEnroll(@Validated @RequestBody ConvenientActivityEnroll resources) {
        convenientActivityEnrollService.create(resources);
        convenientActivityService.updatePersonNum(resources.getActivityId());
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @GetMapping("/findEnrollListByType")
    @Log("查询/api/convenient/activity/enroll/type")
    @ApiOperation("查询/api/convenient/activity/enroll/type")
    public ResponseEntity<PageResult<ConvenientActivityEnrollDto>> findEnrollListByType(ConvenientActivityEnrollQueryCriteria criteria, Pageable pageable) {
        PageResult<ConvenientActivityEnrollDto> convenientActivityDtoPageResult = convenientActivityEnrollService.queryAllByType(criteria, pageable);
        return new ResponseEntity<>(convenientActivityDtoPageResult, HttpStatus.OK);
    }

    @Log("新增/api/convenient/activity")
    @ApiOperation("新增/api/convenient/activity")
    @PostMapping("/create")
    public ResponseEntity<Object> createActivity(@Validated @RequestBody ConvenientActivity resources) {
        convenientActivityService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改/api/convenient/activity")
    @ApiOperation("修改/api/convenient/activity")
    @PostMapping("/update")
    public ResponseEntity<Object> updateActivity(@Validated @RequestBody ConvenientActivity resources) throws Exception {
        convenientActivityService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除/api/convenient/activity")
    @ApiOperation("删除/api/convenient/activity")
    @PostMapping("/delete")
    public ResponseEntity<Object> deleteActivity(@RequestParam Integer ids) {
        convenientActivityService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
